<?php
/**
 * User: Drery
 * Date: 2015/11/18
 * Time: 11:13
 */

namespace api\components;

use Yii;
use yii\base\UserException;
use yii\web\ServerErrorHttpException;

class AppException extends UserException
{
    const ERROR_LICENSE_PLATE_INVALID = 10101;
    const ERROR_USER_REGISTERED = 10102;
    const ERROR_USER_NOT_EXISTS = 10103;
    const ERROR_USER_INFO_NOT_MATCH = 10104;
    const ERROR_USER_FACE_ERROR = 10105;
    const ERROR_PHONE_USED = 10106;
    const ERROR_PASSWORD_ERROR = 10107;
    const ERROR_INVALID_FACE = 10108;
    const ERROR_FACE_FAIL = 10114;
    const ERROR_USER_FORBIDDEN = 10109;
    const ERROR_USER_ABNOMAL = 10110;
    const ERROR_IMAGE_BROKEN = 10111;
    const ERROR_IMAGE_INVALID = 10112;
    const ERROR_VEHICLE_EXPIRED = 10113;
    const ERROR_DRIVING_EXISTS = 10115;
    const ERROR_QUALIFICATION_EXISTS = 10116;
    const ERROR_STUDY_UNFINISHED = 10201;
    const ERROR_EXAM_FINISHED = 10202;
    const ERROR_STUDY_EXPIRED = 10203;
    const ERROR_EXAM_EXPIRED = 10204;
    const ERROR_STUDY_INVALID = 10205;
    const ERROR_TEMP_USER_STOP_SIGNUP = 19901;

    public $errorMaps = [
        self::ERROR_LICENSE_PLATE_INVALID => '您注册的车牌号未在系统中注册',
        self::ERROR_USER_REGISTERED => '已注册，请直接登录',
        self::ERROR_USER_NOT_EXISTS => '用户不存在，请先注册',
        self::ERROR_USER_INFO_NOT_MATCH => '驾驶证与手机号不匹配',
        self::ERROR_USER_FACE_ERROR => '用户与人脸不匹配',
        self::ERROR_PHONE_USED => '该手机号已有人使用',
        self::ERROR_PASSWORD_ERROR => '密码错误',
        self::ERROR_INVALID_FACE => '未检测到人脸，请将人脸对准摄像头后重新检测',
        self::ERROR_FACE_FAIL => '人脸识别失败，请重试',
        self::ERROR_USER_FORBIDDEN => '用户被禁用',
        self::ERROR_USER_ABNOMAL => '当前用户异常，请联系管理员',
        self::ERROR_IMAGE_BROKEN => '未上传图片或图片已损坏，请重新上传',
        self::ERROR_IMAGE_INVALID => '图片过大或格式不可用，请更换图片上传',
        self::ERROR_VEHICLE_EXPIRED => '您所在车辆已到期，为避免影响您的使用，请尽快与您所属运输公司联系。',
        self::ERROR_DRIVING_EXISTS => '此驾驶证已被注册，若有疑问，请联系管理员',
        self::ERROR_QUALIFICATION_EXISTS => '此资格证号已被注册，若有疑问，请联系管理员',
        self::ERROR_STUDY_UNFINISHED => '请先完成安全例会的学习',
        self::ERROR_EXAM_FINISHED => '考核已通过，请勿重复提交',
        self::ERROR_STUDY_EXPIRED => '该安全教育已过期，请查看最新的安全教育',
        self::ERROR_EXAM_EXPIRED => '该学习考核已过期，请查看最新的学习考核',
        self::ERROR_STUDY_INVALID => '该安全教育',
        self::ERROR_TEMP_USER_STOP_SIGNUP => '由于系统内部维护，孟州的用户暂时关闭注册。'
    ];

    public function __construct($code)
    {
        $message = isset($this->errorMaps[$code]) ? $this->errorMaps[$code] : null;
        if(empty($message)) {
            throw new ServerErrorHttpException;
        }
        $message = Yii::t('api/error',$message);
        parent::__construct($message, $code);
    }
}